﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MadeInPortugal.DataSet1TableAdapters;
using System.Collections;

namespace MadeInPortugal
{
    public class TagType
    {
        int tagId;
        int typeId;



        public int TagId
        {
            get { return tagId; }
            set { tagId = value; }
        }
        public int TypeId
        {
            get { return typeId; }
            set { typeId = value; }
        }

        public TagType[] GetTypeId(int tagId)
        {
            TBTagTypeTableAdapter TagTypeTableAdapter = new TBTagTypeTableAdapter();
            DataSet1.TBTagTypeDataTable tagIds = TagTypeTableAdapter.GetDataByTagId(tagId);
            TagType[] ret = new TagType[tagIds.Count];
            int i = 0;
            foreach (DataSet1.TBTagTypeRow tagrow in tagIds)
            {
                //TagType newtag = new TagType {TagId=tagrow.TagID,TypeId=tagrow.TypeID};
                ret[i] = new TagType { TagId = tagrow.TagID, TypeId = tagrow.TypeID };

                i++;
            }
            return ret;
        }
        public TagType[] GetTagId(int typeId)
        {
            TBTagTypeTableAdapter TagTypeTableAdapter = new TBTagTypeTableAdapter();
            DataSet1.TBTagTypeDataTable tagIds = TagTypeTableAdapter.GetData(typeId);
            TagType[] ret = new TagType[tagIds.Count];
            int i = 0;
            foreach (DataSet1.TBTagTypeRow tagrow in tagIds)
            {
                //TagType newtag = new TagType {TagId=tagrow.TagID,TypeId=tagrow.TypeID};
                ret[i] = new TagType { TagId = tagrow.TagID, TypeId = tagrow.TypeID };

                i++;
            }
            return ret;
        }

        public TagType Get(int tagId,int typeId)
        {
            TBTagTypeTableAdapter TagTypeTableAdapter = new TBTagTypeTableAdapter();
            DataSet1.TBTagTypeDataTable tagIds = TagTypeTableAdapter.GetData(typeId);
            TagType ret = new TagType();
            foreach (DataSet1.TBTagTypeRow tagrow in tagIds)
            {
                //TagType newtag = new TagType {TagId=tagrow.TagID,TypeId=tagrow.TypeID};
                ret = new TagType { TagId = tagrow.TagID, TypeId = tagrow.TypeID };


            }
            return ret;
        }

        public int Delete(int tagId)
        {
            TagType Tag = new TagType();
            TBTagTypeTableAdapter TagTypeTableAdapter = new TBTagTypeTableAdapter();
            int ret = TagTypeTableAdapter.Delete(tagId);
            return ret;
        }

        public TagType Insert(int tagID, int typeID)
        {
            TBTagTypeTableAdapter TagTypeTableAdapter = new TBTagTypeTableAdapter();
            TagTypeTableAdapter.Insert(tagID,typeID);
            TagType prod = new TagType();

            return prod.Get(tagID,typeID);
        }
    }
    #region Tag
    public class Tag
    {
        int Id;
        string tag;
        ProductType[] type;

        public int ID
        {
            get { return Id; }
            set { Id = value; }
        }
        public string TagName
        {
            get { return tag; }
            set { tag = value; }
        }
        public ProductType[] Type
        {
            get { return type; }
            set { type = value; }
        }

        public Tag[] GetTagByTypeId(int typeId)
        {
            Tag tags = new Tag();
            ArrayList list = new ArrayList();
            TagType Type = new TagType();
            TagType[] typeList =Type.GetTagId(typeId);
            
            foreach (TagType tagIds in typeList)
            {
                Tag tagList = tags.GetTag(tagIds.TagId);
                //foreach (Tag item in tagList)
                //{
                    list.Add(tagList);
                
                
            } ;
            
            return (Tag[])list.ToArray(typeof (Tag)) ;
        }

        public Tag GetTagByProductId(int productId)
        {
            Tag ret = new Tag();
            TBProductTagTableAdapter ProductTagTableAdapter = new TBProductTagTableAdapter();
            DataSet1.TBProductTagDataTable ProductTagIds = ProductTagTableAdapter.GetData(productId);
            foreach (DataSet1.TBProductTagRow tagRow in ProductTagIds)
            {
                ret = ret.GetTag(tagRow.TagID);
            }

        
            return ret;
        }

        public Tag GetTag(int tagId)
        {
            TBTagTableAdapter TagTableAdapter = new TBTagTableAdapter();
            TBTagTypeTableAdapter TagTypeTableAdapter = new TBTagTypeTableAdapter();
            DataSet1.TBTagDataTable tagIds = TagTableAdapter.GetData(tagId);
            Tag ret = new Tag();
            int i = 0;
            foreach (DataSet1.TBTagRow tagrow in tagIds)
            {
                ret = new Tag { ID = tagrow.ID, TagName = tagrow.Tag };

                DataSet1.TBTagTypeDataTable TagTypeTable = TagTypeTableAdapter.GetDataByTagId(tagrow.ID);
                foreach (DataSet1.TBTagTypeRow TagTypeRow in TagTypeTable)
                {
                    ProductType type = new ProductType();
                    ret.Type = type.GetType(TagTypeRow.TypeID);
                }

                i++;
            }
            return ret;
        }

        public Tag[] GetAllTags()
        {
            TBTagTableAdapter TagTableAdapter = new TBTagTableAdapter();
            DataSet1.TBTagDataTable TagIds = TagTableAdapter.GetAllData();
            Tag[] ret = new Tag[TagIds.Count];
            int i = 0;
            foreach (DataSet1.TBTagRow Tagrow in TagIds)
            {
                ret[i] = new Tag { ID = Tagrow.ID, TagName = Tagrow.Tag };
                i++;
            }
            return ret;
        }
        public Tag Get(string tagName)
        {
            Tag Tag = new Tag();
            TBTagTableAdapter TagTableAdapter = new TBTagTableAdapter();
            DataSet1.TBTagDataTable TagTable = TagTableAdapter.GetDataByTagName(tagName);
            foreach (DataSet1.TBTagRow TagRow in TagTable)
            {
                Tag.ID = TagRow.ID;
                Tag.TagName = TagRow.Tag;
            }
            return Tag;
        }

        public Tag Get(int tagId)
        {
            Tag Tag = new Tag();
            TBTagTableAdapter TagTableAdapter = new TBTagTableAdapter();
            DataSet1.TBTagDataTable TagTable = TagTableAdapter.GetData(tagId);
            foreach (DataSet1.TBTagRow TagRow in TagTable)
            {
                Tag.ID = TagRow.ID;
                Tag.TagName = TagRow.Tag;
            }
            return Tag;
        }

        public int Delete(int tagId)
        {
            TagType typelink = new TagType();
            typelink.Delete(tagId);
            TBTagTableAdapter TagTableAdapter = new TBTagTableAdapter();
            int ret = TagTableAdapter.Delete(tagId);

            return ret;
        }

        public Tag Insert(string tagName)
        {
            TBTagTableAdapter TagTableAdapter = new TBTagTableAdapter();
            TagTableAdapter.Insert(tagName);
            Tag prod = new Tag();

            return prod.Get(tagName);
        }

        public Tag Update(Tag tag)
        {
            TBTagTableAdapter TagTableAdapter = new TBTagTableAdapter();
            TagTableAdapter.Update(tag.TagName,tag.ID);

            return tag;
        }

    }
    #endregion

    #region Product Type
    public class ProductType
    {
        int Id;
        string type;

        public int ID
        {
            get { return Id; }
            set { Id = value; }
        }
        public string Type
        {
            get { return type; }
            set { type = value; }
        }


        public ProductType[] GetType(int tagId)
        {
            TBTypeTableAdapter TypeTableAdapter = new TBTypeTableAdapter();
            DataSet1.TBTypeDataTable TypeIds = TypeTableAdapter.GetData(tagId);
            ProductType[] ret = new ProductType[TypeIds.Count];
            int i = 0;
            foreach (DataSet1.TBTypeRow Typerow in TypeIds)
            {
                ret[i] = new ProductType { ID = Typerow.TypeId, Type = Typerow.Type };
                i++;
            }
            return ret;
        }

        public ProductType[] GetAllType()
        {
            TBTypeTableAdapter TypeTableAdapter = new TBTypeTableAdapter();
            DataSet1.TBTypeDataTable TypeIds = TypeTableAdapter.GetAllData();
            ProductType[] ret = new ProductType[TypeIds.Count];
            int i = 0;
            foreach (DataSet1.TBTypeRow Typerow in TypeIds)
            {
                ret[i] = new ProductType { ID = Typerow.TypeId, Type = Typerow.Type };
                i++;
            }
            return ret;
        }
        public ProductType Get(string typeName)
        {
            ProductType type = new ProductType();
            TBTypeTableAdapter TypeTableAdapter = new TBTypeTableAdapter();
            DataSet1.TBTypeDataTable typeTable = TypeTableAdapter.GetDataBName(typeName);
            foreach (DataSet1.TBTypeRow typeRow in typeTable)
            {
                type.ID = typeRow.TypeId;
                type.Type = typeRow.Type;
            }
            return type;
        }

        public ProductType Get(int typeId)
        {
            ProductType type = new ProductType();
            TBTypeTableAdapter TypeTableAdapter = new TBTypeTableAdapter();
            DataSet1.TBTypeDataTable typeTable = TypeTableAdapter.GetData(typeId);
            foreach (DataSet1.TBTypeRow typeRow in typeTable)
            {
                type.ID = typeRow.TypeId;
                type.Type = typeRow.Type;
            }
            return type;
        }

        public int Delete(int typeId)
        {
            ProductType type = new ProductType();
            TBTypeTableAdapter TypeTableAdapter = new TBTypeTableAdapter();
            int ret = TypeTableAdapter.Delete(typeId);

            return ret;
        }

        public ProductType Insert(string tagName)
        {
            TBTypeTableAdapter TypeTableAdapter = new TBTypeTableAdapter();
            TypeTableAdapter.Insert(tagName);
            ProductType prod = new ProductType();

            return prod.Get(tagName);
        }
        
        public ProductType Update(ProductType prodType)
        {
            TBTypeTableAdapter TypeTableAdapter = new TBTypeTableAdapter();
            TypeTableAdapter.Update(prodType.Type,prodType.ID);
            return prodType;
        }
    }

    #endregion

    public class ProductTag
    {
        int tagId;
        int productId;



        public int TagId
        {
            get { return tagId; }
            set { tagId = value; }
        }
        public int ProductId
        {
            get { return productId; }
            set { productId = value; }
        }

        public ProductTag[] GetProductId(int tagId)
        {
            TBProductTagTableAdapter ProductTagTableAdapter = new TBProductTagTableAdapter();
            DataSet1.TBProductTagDataTable tagIds = ProductTagTableAdapter.GetDataByTagId(tagId);
            ProductTag[] ret = new ProductTag[tagIds.Count];
            int i = 0;
            foreach (DataSet1.TBProductTagRow tagrow in tagIds)
            {
                //TagType newtag = new TagType {TagId=tagrow.TagID,TypeId=tagrow.TypeID};
                ret[i] = new ProductTag { TagId = tagrow.TagID,ProductId = tagrow.ProductID };

                i++;
            }
            return ret;
        }

        public ProductTag[] GetTagId(int productId)
        {
            TBProductTagTableAdapter ProductTagTableAdapter = new TBProductTagTableAdapter();
            DataSet1.TBProductTagDataTable tagIds = ProductTagTableAdapter.GetData(productId);
            ProductTag[] ret = new ProductTag[tagIds.Count];
            int i = 0;
            foreach (DataSet1.TBProductTagRow tagrow in tagIds)
            {
                //TagType newtag = new TagType {TagId=tagrow.TagID,TypeId=tagrow.TypeID};
                ret[i] = new ProductTag { TagId = tagrow.TagID, ProductId = tagrow.ProductID };

                i++;
            }
            return ret;
        }

        public ProductTag Get(int tagId, int typeId)
        {
            TBProductTagTableAdapter ProductTagTableAdapter = new TBProductTagTableAdapter();
            DataSet1.TBProductTagDataTable tagIds = ProductTagTableAdapter.GetData(typeId);
            ProductTag ret = new ProductTag();
            foreach (DataSet1.TBProductTagRow tagrow in tagIds)
            {
                //ProductTag newtag = new ProductTag {TagId=tagrow.TagID,TypeId=tagrow.TypeID};
                ret = new ProductTag { TagId = tagrow.TagID, ProductId = tagrow.ProductID };


            }
            return ret;
        }

        public int Delete(int productId)
        {
            ProductTag Tag = new ProductTag();
            TBProductTagTableAdapter ProductTagTableAdapter = new TBProductTagTableAdapter();
            int ret = ProductTagTableAdapter.Delete(productId);
            return ret;
        }

        public ProductTag Insert(int tagID, int productId)
        {
            TBProductTagTableAdapter ProductTagTableAdapter = new TBProductTagTableAdapter();
            ProductTagTableAdapter.Insert(productId,tagID);
            ProductTag prod = new ProductTag();

            return prod.Get(tagID, productId);
        }
    }
}
    